package com.hello.games;

import java.io.IOException;
import java.util.Random;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.WallpaperManager;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.PorterDuff.Mode;
import android.graphics.drawable.Drawable;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.text.format.Time;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.hello.R;
import com.hello.resources.SoundManager;

public class PressTheButtonGame extends Activity{
	
	private SoundManager mSoundManager;// in order to add sound to buttons
	MediaPlayer MP;// background sound
	Time StartTime;
	Time EndTime;
	public void setWallpaper() {
		Drawable wallpaper = WallpaperManager.getInstance(this).getDrawable();
        if (wallpaper == null) {
            try {
                clearWallpaper();
            } catch (IOException e) {
              Toast.makeText(this, e.getMessage(), 3).show();
            }
        } else {
        	wallpaper.setColorFilter(R.color.gray,Mode.DARKEN);
            getWindow().setBackgroundDrawable(wallpaper);
        }
	}
	private boolean isGreen = false;
	Button button;
	private boolean drunk=false;
	private int attempt=0;
	private static final int GAME_LIMIT=3; //the number of games allowed

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pressthebtngame);
		setWallpaper();
		StartTime = new Time();
		EndTime = new Time();
		mSoundManager = new SoundManager();
		mSoundManager.initSounds(getBaseContext());
		mSoundManager.addSound(1, R.raw.comedy_zips_pops_and_bonks);
		mSoundManager.addSound(2, R.raw.sound);
		
        button = (Button) findViewById(R.id.BigBtn);
       
		MP=MediaPlayer.create(getBaseContext(), R.raw.black_paper);
		MP.setLooping(true);
		MP.start();

		OnClickListener btnClick = new OnClickListener() { //This listener is used when a card is clicked.

			
			public void onClick(View v) {
				EndTime.setToNow();
				long delayTime =  EndTime.toMillis(true) - StartTime.toMillis(true);
				mSoundManager.playSound(1);
				if (isGreen){
					attempt++;
					if(delayTime < 600){
						drunk = false;
					}else{
						drunk = true;
					}
					delayShowMessage();
				}
				else{
					drunk=true;
					delayShowMessage("You Must Wait For The Button To Turn Green.\n" +
							"You Must Be Drunk.");
				}
				
				
				

			}
		};
		
		button.setOnClickListener(btnClick);
		Handler handler = new Handler();
		handler.postDelayed(new Runnable() { 
			public void run() { 
				startGame();
			} 
		}, 4000);
		
	}

	public void delayNextStep(){
		Handler handler = new Handler();
		handler.postDelayed(new Runnable() { 
			public void run() { 
				PressTheButtonGame.this.doNextStep();
			} 
		}, 200);
	}

	public void delayShowMessage(){
		Handler handler = new Handler();
		handler.postDelayed(new Runnable() { 
			public void run() { 
				PressTheButtonGame.this.showMessage();
			} 
		}, 0);
	}
	
	public void delayShowMessage(final String Msg){
		Handler handler = new Handler();
		handler.postDelayed(new Runnable() { 
			public void run() { 
				PressTheButtonGame.this.showMessage(Msg);
			} 
		}, 100);
	}


	private void startGame() {
		button.setBackgroundDrawable(getBaseContext().getResources().getDrawable(R.drawable.redbtn));
		isGreen = false;
		Random rand  = new Random();
		int start = rand.nextInt(3000) + 3000;
		delayReplaceBtn(start);
	}
	
	private void delayReplaceBtn(int time) { //waits short amount of time before hiding the animals
		Handler handler = new Handler();
		handler.postDelayed(new Runnable() { 
			public void run() {
				button.setBackgroundDrawable(getBaseContext().getResources().getDrawable(R.drawable.greenbtn));
				isGreen = true;
				StartTime.setToNow();
			} 
		}, time);
	}

	@Override
	public boolean onSearchRequested() { 
		return false; //since the search button resets the game, do nothing
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent msg) {
		if ((keyCode == KeyEvent.KEYCODE_MENU) || (keyCode == KeyEvent.KEYCODE_SEARCH)) { //menu or search button pressed

		} else if (keyCode == KeyEvent.KEYCODE_BACK) {
			finish();
		} else { //otherwise, do the key's original action
			super.onKeyDown(keyCode, msg);
		}
		return true;
	}

	public void showMessage() {
		String message="";
		if (!drunk) { //show game won message
			message=getString(R.string.YoureNotDrunkMessage);
			//new AlertDialog.Builder(this).setTitle(getString(R.string.win_title)).setMessage(getString(R.string.YoureNotDrunkMessage)).setPositiveButton(getString(R.string.ok), null).show();
		} else {
			switch (attempt){
			case 1:
				message=getString(R.string.firstFailGameMessage);
				break;
			case 2:
				message=getString(R.string.secondFailGameMessage);
				break;
			case 3:
				message=getString(R.string.youreDrunkMessage);
				break;    			
			}


		}
		if (drunk){
			message  = "You Are Too Slow\n"+ message;
		}
		android.content.DialogInterface.OnClickListener okClick= new android.content.DialogInterface.OnClickListener() {

			
			public void onClick(DialogInterface dialog, int which) {
				PressTheButtonGame.this.delayNextStep();
			}
		};
		new AlertDialog.Builder(this).setTitle(getString(R.string.lose_title)).setMessage(message).setPositiveButton(getString(R.string.ok), okClick).show();
	}
	
	public void showMessage(String message) {
		android.content.DialogInterface.OnClickListener okClick= new android.content.DialogInterface.OnClickListener() {

			
			public void onClick(DialogInterface dialog, int which) {
				PressTheButtonGame.this.delayNextStep();
			}
		};
		new AlertDialog.Builder(this).setTitle(getString(R.string.lose_title)).setMessage(message).setPositiveButton(getString(R.string.ok), okClick).show();
	}
	
	public boolean getDrunkOutput(){
		return drunk;

	}

	@Override
	public void finish(){
			MP.stop(); //stops the background music
			super.finish();
		
	}

	public void doNextStep() {
		if(!drunk){
			Intent intent = new Intent();
			setResult(RESULT_OK, intent);
			finish();
		}else if(attempt<GAME_LIMIT){
			startGame();
		}else{
			Intent intent = new Intent();
			setResult(RESULT_CANCELED, intent);
			finish();
		}
	}

}
